#include "tcp.h"
#include <func.h>

int main()
{
    pthreadpoll_t* pthreadpoll = (pthreadpoll_t*)calloc(MAXEVENTS,sizeof(pthreadpoll_t));
    pthreadpollinit(pthreadpoll);
    int arrfd[3]={-1};
    int listenfd = socket(AF_INET,SOCK_STREAM,0);
    int reuse = 1;
    if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0){
        perror("setsockopt");
    }
    struct sockaddr_in serveraddr;
    memset(&serveraddr,0,sizeof(serveraddr));
    serveraddr.sin_family=AF_INET;
    serveraddr.sin_port=htons(8888);
    serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    bind(listenfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
    listen(listenfd,10);
    int epfd = epoll_create1(0);
    struct epoll_event* peventlist = (struct epoll_event*)calloc(10,sizeof(struct epoll_event));
    struct epoll_event evt;
    evt.data.fd=listenfd;
    evt.events=EPOLLIN;
    epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&evt);
    char buf[1024]={0};
    memset(buf,0,1024);
    char filename[1024]={0};
    char order[1024]={0};
    memset(filename,0,sizeof(filename));
    memset(order,0,sizeof(order));
    int q = 0;
    while(1){
        int ret = epoll_wait(epfd,peventlist,10,5000);
        if(ret == 0 ){
            printf("epoll_wait timeout\n");
        }else if(ret == -1 && errno == EINTR){
            continue;
        }else if(ret == -1 ){
            perror("epoll_wait");
        }else {
            for(int i = 0;i<ret;++i){
                int fd = peventlist[i].data.fd;
                if(fd == listenfd){
                    struct sockaddr_in clientaddr;
                    memset(&clientaddr,0,sizeof(clientaddr));
                    socklen_t len = sizeof(clientaddr);
                    int peerfd = accept(listenfd,(struct sockaddr*)&clientaddr,&len);
                    int ret = examinePasswd(peerfd);
                    if(ret){
                    int flags = fcntl(peerfd,F_GETFL,0);
                    flags|=O_NONBLOCK;
                    fcntl(peerfd,F_GETFL,flags);
                    struct epoll_event evt;
                    evt.data.fd=peerfd;
                    arrfd[q]=peerfd;
                    q++;
                    evt.events=EPOLLIN;
                    epoll_ctl(epfd,EPOLL_CTL_ADD,peerfd,&evt);
                    printf("access connect a new client,ip:%s,port:%d.\n",
                           inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));            
                    }
                }else{
                    if(peventlist[i].events & EPOLLIN){
                        memset(filename,0,sizeof(filename));
                        memset(order,0,sizeof(order));
                        int u = 0,x=0,z=0,t=0;//获取命令及文件名的参数
                        int flag = 0;//是否输入了文件名的标志位;
                        printf("EPOLLIN event\n");
                        memset(buf,0,1024);
                        int ret = recv(fd,buf,sizeof(buf),0);
                        if(ret == 0){
                            printf("clientfd:%d is close\n",fd);
                            close(fd);
                        }
                        printf("recv:%s\n",buf);
                        if(ret>0){
                            for(;z<ret;z++){
                                while(buf[z]!=32&&z<ret-1){
                                    order[u]=buf[z];
                                    z++;
                                    u++;
                                    if(buf[z]==32||z==ret-1){
                                        t=z;
                                        z=ret;
                                    }
                                }
                            }
                            puts(order);
                            for(;t<ret;t++){
                                while(buf[t]!=32&&t<ret-1){
                                    filename[x]=buf[t];
                                    x++;
                                    t++;
                                    if(buf[t]==32||t==ret-1){
                                        flag = 1;
                                        t = ret;
                                    }
                                }
                            }
                            if(flag){
                                puts(filename);//标志位置为1时，说明输入了文件名，执行改代码
                            }
                        }
                        dowork(pthreadpoll,fd,filename,order,epfd); 


                    }
                }
            }
        }
    }


    return 0;
}